package javaServlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class zhuceServlet
 */
@WebServlet("/zhuceServlet")
public class zhuceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public zhuceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8"); 
        String one=request.getParameter("one");
        if("1".equals(one)){
		response.setHeader("Content-Disposition", "attachment;filename=123.txt");
		String path=this.getServletContext().getRealPath("/123.txt");
		FileInputStream fis=new FileInputStream(path);
		byte[] buff=new byte[1024];
		int len=0;
		OutputStream os=response.getOutputStream();
		while((len=fis.read(buff))>0)
		{
			 os.write(buff,0,len);
		}
		os.close();
		fis.close();
        }
        else if("2".equals(one)){
        	
        	response.setHeader("Content-Disposition", "attachment;filename=pict.jpeg");
    		String path=this.getServletContext().getRealPath("/pict.jpeg");
    		FileInputStream fis=new FileInputStream(path);
    		byte[] buff=new byte[1024];
    		int len=0;
    		OutputStream os=response.getOutputStream();
    		while((len=fis.read(buff))>0)
    		{
    			 os.write(buff,0,len);
    		}
    		os.close();
    		fis.close();
            }
        }
		
		

		
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
